<template>
  <el-container class="homeContainer">
    <!-- 头部区域 -->
    <el-header>
      <div class="log">
        <img src="../assets/logo.png" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logOut">退出</el-button>
    </el-header>
    <!-- 容器区域 -->
    <el-container>
      <!-- 左侧边栏区域 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!-- 控制折叠and展开 -->
        <div class="toggleElMenu" @click="toggleCollapse">|||</div>
        <!-- 侧边栏菜单区域 -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409Eff"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="(item, icnIndex) in menuList"
            :key="item.id"
          >
            <!-- 一级菜单模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconList[icnIndex]"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + childItem.path"
              v-for="childItem in item.children"
              :key="childItem.id"
              @click="saveNavState('/' + childItem.path)"
            >
              <!-- 二级菜单模板区域 -->
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ childItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右主体区域 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import '../assets/css/global.css';

export default {
  data() {
    return {
      menuList: [], //左侧菜单数据
      iconList: [
        'el-icon-s-custom',
        'el-icon-bangzhu',
        'el-icon-s-cooperation',
        'el-icon-s-order',
        'el-icon-s-marketing',
      ], //循环一级菜单的图标
      isCollapse: false, //用动态判断是否折叠状态保存
      activePath: '', //保存激活的链接地址
    };
  },
  created() {
    //生命周期函数页面创建就会执行页面销毁时销毁
    this.getMenuList(); //保存token
    this.activePath = window.sessionStorage.getItem('activePath'); //取出存到页面的激活值
  },
  methods: {
    logOut() {
      window.sessionStorage.clear(); //点击退出按钮后清空浏览器存储的token
      this.$router.push('/login');
    },
    async getMenuList() {
      //所有的菜单
      const { data: response } = await this.$http.get('menus');
      if (response.meta.status !== 200)
        //当请求失败返回提示
        return this.$message({
          message: response.meta.msg,
          type: 'error',
          center: true,
        }); //当成功把值赋值给data的menuList
      this.menuList = response.data;
    },
    toggleCollapse() {
      //点击按钮切换折叠与展开
      this.isCollapse = !this.isCollapse;
    },
    saveNavState(statePath) {
      //保存链接的激活状态
      window.sessionStorage.setItem('activePath', statePath);
      this.activePath = statePath//每次点击重新赋值
    },
  },
};
</script>

<style scoped>
.el-header {
  display: flex;
  color: #fff;
  padding-left: 0;
  font-size: 22px;
  align-items: center;
  background-color: #373d41;
  justify-content: space-between;
}

.log {
  height: 100%;
}

.log img {
  height: 100%;
  transition: all 1s;
  padding-right: 18px;
  vertical-align: middle;
}

.log img:hover {
  transform: rotate(360deg);
}

.el-aside {
  background-color: #333744;
}

.el-menu {
  border-right: 0;
}

.el-main {
  background-color: #eaedf1;
}

.toggleElMenu {
  height: 30px;
  color: #fff;
  font-size: 10px;
  cursor: pointer;
  line-height: 30px;
  text-align: center;
  letter-spacing: 0.2em;
  background-color: #4a5064;
}
</style>